package com.liukai.exam.controller;


import com.liukai.exam.common.Result;
import com.liukai.exam.pojo.ExamManage;
import com.liukai.exam.pojo.exam.QueryForm;
import com.liukai.exam.service.IExamManageService;
import com.liukai.exam.utils.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.ValidationEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试卷管理 前端控制器
 * </p>
 *
 * @author liu kai
 * @since 2021-02-01
 */
@RestController
@RequestMapping("/exam/examManage")
@Slf4j
public class ExamManageController {

    @Autowired
    private IExamManageService iExamManageService;

    /**
     * 新增试卷信息
     *
     * @param examManage
     * @return
     */
    @PostMapping("/addExamManage")
    public Result addExamManage(@RequestBody ExamManage examManage) {

        log.info(examManage.toString());
        int count = iExamManageService.addExamManage(examManage);
        log.info("count" + count);
        if (count > 0) {
            ExamManage newExamManage = iExamManageService.findExamNameByName(examManage.getTeacherId(),
                    examManage.getClassId(), examManage.getExamName());
            Result result = Result.succ(200, "新增试卷信息成功", newExamManage);
            return result;
        } else if (count == Constant.EXAM_MANAGE_ADD_FILED_NAME_EXIST) {
            Result result = Result.fail(404, "新增试卷信息失败,试卷名称已存在", null);
            return result;
        } else {
            Result result = Result.fail(404, "新增试卷信息失败", null);
            return result;
        }
    }

    /**
     * 根据账号信息查询未完成创建的试卷信息
     *
     * @param queryForm
     * @return
     */
    @GetMapping("/findNoDoneCreateExam")
    public Result findNoDoneCreateExam(QueryForm queryForm) {

        Long userId = queryForm.getUserId();
        Integer classId = queryForm.getClassId();

        log.info("findNoDoneCreateExam");
        // 获取该账号信息的所有未完成的试卷信息
        List<ExamManage> noDoneCreateExam = iExamManageService.findNoDoneCreateExam(userId, classId);
        // 条件、分页查询
        Map<String, Object> resultMap = iExamManageService.getExamManageByCondition(queryForm, noDoneCreateExam);

        return Result.succ(200, "获取未完成创建的试卷成功", resultMap);
    }

    /**
     * 根据账号信息查询已完成创建的试卷信息
     *
     * @param queryForm
     * @return
     */
    @RequestMapping("/findDoneCreateExam")
    public Result findDoneCreateExam(QueryForm queryForm) {

        System.out.println("111111111111111111111111");

        Long userId = queryForm.getUserId();
        Integer classId = queryForm.getClassId();

        System.out.println(queryForm.toString());

        log.info("findDoneCreateExam");
        List<ExamManage> doneCreateExam = iExamManageService.findDoneCreateExam(userId, classId);

        return Result.succ(200, "获取已经完成创建的试卷成功", doneCreateExam);
    }

    @DeleteMapping("/deleteExamManageById")
    public Result deleteExamManageById(ExamManage examManage){

//        ExamManage examManage = new ExamManage();
//        examManage.setTeacherId(teacherId);
//        examManage.setClassId(classId);
//        examManage.setExamName(examName);
        System.out.println(examManage.toString());

        int count = iExamManageService.deleteExamManageById(examManage);

        if (count > 1){
            return Result.succ("删除成功");
        }else{
            return Result.fail("删除失败");
        }

    }

}

